package com.hanthink.system.service.impl;

import java.util.List;

import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.manhour.ProductionPlanInShift;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hanthink.system.mapper.EmRoasterMapper;
import com.hanthink.system.domain.EmRoaster;
import com.hanthink.system.service.IEmRoasterService;

/**
 * -Service业务层处理
 * 
 * @author hanthink
 * @date 2022-04-14
 */
@Service
public class EmRoasterServiceImpl implements IEmRoasterService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);


    @Autowired
    private EmRoasterMapper emRoasterMapper;

    /**
     * 查询-
     * 
     * @param id -ID
     * @return -
     */
    @Override
    public EmRoaster selectEmRoasterById(String id)
    {
        return emRoasterMapper.selectEmRoasterById(id);
    }

    /**
     * 查询-列表
     * 
     * @param emRoaster -
     * @return -
     */
    @Override
    public List<EmRoaster> selectEmRoasterList(EmRoaster emRoaster)
    {
        return emRoasterMapper.selectEmRoasterList(emRoaster);
    }

    /**
     * 新增-
     * 
     * @param emRoaster -
     * @return 结果
     */
    @Override
    public int insertEmRoaster(EmRoaster emRoaster)
    {
        return emRoasterMapper.insertEmRoaster(emRoaster);
    }

    /**
     * 修改-
     * 
     * @param emRoaster -
     * @return 结果
     */
    @Override
    public int updateEmRoaster(EmRoaster emRoaster)
    {
        return emRoasterMapper.updateEmRoaster(emRoaster);
    }

    /**
     * 批量删除-
     * 
     * @param ids 需要删除的-ID
     * @return 结果
     */
    @Override
    public int deleteEmRoasterByIds(String[] ids)
    {
        return emRoasterMapper.deleteEmRoasterByIds(ids);
    }

    /**
     * 删除-信息
     * 
     * @param id -ID
     * @return 结果
     */
    @Override
    public int deleteEmRoasterById(String id)
    {
        return emRoasterMapper.deleteEmRoasterById(id);
    }

    @Override
    public String importEmRoasteritem(List<EmRoaster> EmRoasterItemList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(EmRoasterItemList) || EmRoasterItemList.size() == 0) {
            throw new CustomException("导入的生产排班表不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int currentRowNum = 0;
        for (EmRoaster EmRoasterItem : EmRoasterItemList) {
            currentRowNum++;
            try {
                EmRoaster items = emRoasterMapper.selectEmRoasterById(EmRoasterItem.getId());//.getPlanworkdatetime(),prdPlanItem.getPshiftid(),prdPlanItem.getMachineid() ,prdPlanItem.getMaterialid()) ;
                if (StringUtils.isNull(items)) {
                    EmRoasterItem.setCreateBy(operName);
                    EmRoasterItem.setId(java.util.UUID.randomUUID().toString());
                    this.insertEmRoaster(EmRoasterItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、生产班组" + EmRoasterItem.getFactoryid() + EmRoasterItem.getCreatedatetime() +EmRoasterItem.getPlandatetime()+EmRoasterItem.getShiftid() +EmRoasterItem.getTeamid()+ " 导入成功");
                } else if (isUpdateSupport) {
                    EmRoasterItem.setUpdateBy(operName);
                    this.updateEmRoaster(EmRoasterItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、生产班组 " + EmRoasterItem.getFactoryid() + EmRoasterItem.getCreatedatetime() +EmRoasterItem.getPlandatetime()+EmRoasterItem.getShiftid() +EmRoasterItem.getTeamid() + " 更新成功");
                    continue;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、生产班组 " + EmRoasterItem.getFactoryid() + EmRoasterItem.getCreatedatetime() +EmRoasterItem.getPlandatetime()+EmRoasterItem.getShiftid() +EmRoasterItem.getTeamid() + " 已存在");
                    continue;
                }
            }catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、生产班组 " + EmRoasterItem.getFactoryid() + EmRoasterItem.getCreatedatetime() +EmRoasterItem.getPlandatetime()+EmRoasterItem.getShiftid() +EmRoasterItem.getTeamid() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
